//
//  BIZBusinessPremisesListVC.m
//  biz
//
//  Created by 小龙 on 2017/3/18.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZBusinessPremisesListVC.h"
#import "BIZBussinessPremissCell.h"
#import "BIZOfficeModel.h"
#import "BIZBusinessPremisesDetailVC.h"

@interface BIZBusinessPremisesListVC ()
/** 事务所列表 */
@property (nonatomic, strong) NSMutableArray *officeModelArr;
@property (nonatomic, assign) NSInteger lastId;
@end

@implementation BIZBusinessPremisesListVC

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self setupNavBar];
    [self setupTableView];
    [self getOfficeModelArrData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.officeModelArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    BIZHomebusinessPremisesCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"BIZHomebusinessPremisesCell" forIndexPath:indexPath];
    
    BIZBussinessPremissCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BIZBussinessPremissCell" forIndexPath:indexPath];
    cell.officeModel = self.officeModelArr[indexPath.row];
    cell.recommendIcon.hidden = YES;
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BIZBusinessPremisesDetailVC *vc = [BIZBusinessPremisesDetailVC viewControllerFromStoryboard];
    vc.officeModel = self.officeModelArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)setupNavBar
{
    self.title = @"事务所专区";
}

- (void)setupTableView
{
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
//    self.tableView.sectionHeaderHeight = 10;
//    self.tableView.sectionFooterHeight = 0;
//    self.tableView.contentInset = UIEdgeInsetsMake(11-35, 0, 0, 0);
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tableView  setSeparatorColor:COLOR_UNLINE_GREY];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BIZBussinessPremissCell class]) bundle:nil] forCellReuseIdentifier:@"BIZBussinessPremissCell"];
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, 10)];
    headerView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headerView;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.lastId = 1;
        [self.officeModelArr removeAllObjects];
        [self getOfficeModelArrData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter  footerWithRefreshingBlock:^{
        [self getOfficeModelArrData];
    }];
    
}

- (void)stopRefresh
{
    !self.tableView.mj_header.isRefreshing ? : self.tableView.mj_header.endRefreshing;
    !self.tableView.mj_footer.isRefreshing ? : self.tableView.mj_footer.endRefreshing;
}


- (void)getOfficeModelArrData
{
    NSMutableDictionary *paramsDic = [NSMutableDictionary dictionary];
    paramsDic[@"pageIndex"] = @(self.lastId);
    
    paramsDic[@"pageEnd"] = @(self.lastId+9);
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"test"] = @2;
    
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kBusinessPremisesListUrl parameters:paramsDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"更多事务所列表responseObject：%@", responseObject);
    
        BIZOfficeArrModel *model = [BIZOfficeArrModel yy_modelWithJSON:responseObject];
        
        [self stopRefresh];
        
        if (model.code == 1) {
            [MBProgressHUD hideHUD];
            [self.officeModelArr addObjectsFromArray:model.officeModelArr];
            self.lastId += model.officeModelArr.count;
            [self.tableView reloadData];

        } else {
            [MBProgressHUD showErrorMessage:model.des];
            
            if (model.code == 2) {
                BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
                MPWeakSelf(self);
                vc.loginSuccessBlock = ^(){
                    [weakself getOfficeModelArrData];
                };
                [self presentViewController:vc animated:YES completion:nil];
            }
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"更多事务所列表error：%@", error);
        [self stopRefresh];
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }

    }];
}


-(NSMutableArray *)officeModelArr
{
    if (!_officeModelArr) {
        _officeModelArr = [NSMutableArray array];
    }
    
    return _officeModelArr;
}


- (NSInteger)lastId
{
    if (!_lastId) {
        _lastId = 1;
    }
    
    return _lastId;
}



@end
